const app = getApp();
Component({
  properties: {
    extClass: {
      type: String,
      value: ''
    },
    background: {
      type: String,
      value: 'rgba(255, 255, 255, 1)',
      observer: '_showChange'
    }
  },
  data: {
    pictureUrl: '/image/',
    levelFlag: false,
    userInfo: app.globalData.userInfo || {}
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      wx.getUserInfo({
        success: res => {
          this.setData({
            userInfo: res.userInfo
          })
        },
        fail: err => {
          this.login()
        },
      })
      
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
    //授权点击取消
    cancle() {
      this.setData({
        levelFlag: false
      })
    },
    // 点击登录按钮
    login(e) {
      console.log(e)
      if (app.globalData.userInfo.nickName) {
        this.setData({
          userInfo: app.globalData.userInfo,
          levelFlag: false
        }, this.loadInfo)
      } else if(e){
        if(e.detail.userInfo){
          let msg = {
            encrypteddata: e.detail.encryptedData,
            iv: e.detail.iv
          }
          app.globalData.userInfo = e.detail.userInfo
          this.setData({
            userInfo: e.detail.userInfo,
            levelFlag: false
          })
          // 暂时变更
        app.request("update_userinfo2", msg, true).then(res => {
          console.warn("首页上传用户信息")
          console.log(res)
        })
        }else{
          this.setData({
            userInfo: {},
            levelFlag: false
          })
        }
        
      }else {
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              levelFlag: false
            }, this.loadInfo)
            let msg = {
              encrypteddata: res.encryptedData,
              iv: res.iv
            }
            // 暂时变更
            app.request("update_userinfo2", msg, true).then(res => {
              console.warn("首页上传用户信息")
              console.log(res)
            })
          },
          fail: res => {
            console.log("拒绝")
          }
        })
      }
    },
    // 社保代缴
    payfor() {
      app.aldstat.sendEvent('首页-社保代缴', '首页-社保代缴');
      console.log(this.data.userInfo)
      if (this.data.userInfo.nickName || app.globalData.userInfo.nickName) {
        wx.showLoading({
          title: '等待中',
        })
        wx.navigateTo({
          url: `../paySocial/paySocial`,
        }, wx.hideLoading())
      } else {
        this.setData({
          levelFlag: true
        })
      }
    },
    // 公积金代缴
    providentfund() {
      // 公积金代缴
      if (this.data.userInfo.nickName || app.globalData.userInfo.nickName) {
        wx.showLoading({
          title: '等待中',
        })
        wx.navigateTo({
          url: `../payGold/payGold`,
        }, wx.hideLoading())
      } else {
        this.setData({
          levelFlag: true
        })
      }
      app.aldstat.sendEvent('首页-公积金代缴', '首页-公积金代缴');
    },
    // 提取公积金
    extract() {
      if (this.data.userInfo.nickName || app.globalData.userInfo.nickName) {
        wx.showLoading({
          title: '等待中',
        })
        app.request("withdraw_cash/create_order", {}, true).then(res => {
          wx.hideLoading()
          if (res.error_code == 0) {
            wx.navigateTo({
              url: `../money_pinkup/pages/goldmsg/goldmsg?order_id=${res.data.order_id}`,
            })
          }else{
            wx.showModal({
              content: res.error_message,
              showCancel:false
            })
          }
        })
      } else {
        this.setData({
          levelFlag: true
        })
      }
    },
    // 去领取
    receive() {
      if (this.data.userInfo || app.globalData.userInfo) {
        wx.showLoading({
          title: '加载中',
        })
        wx.navigateTo({
          url: '../distribution/distribution',
        }, wx.hideLoading())
      } else {
        this.setData({
          levelFlag: true
        })
      }
    },
  }
});